package com.wgb.movie.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wgb.movie.common.ErrorCode;
import com.wgb.movie.constant.CommonConstant;
import com.wgb.movie.exception.ThrowUtils;
import com.wgb.movie.mapper.MovieMapper;
import com.wgb.movie.model.dto.movie.MovieQueryRequest;
import com.wgb.movie.model.entity.Movie;

import com.wgb.movie.model.entity.MovieShow;
import com.wgb.movie.model.vo.MovieVO;

import com.wgb.movie.service.MovieOrderService;
import com.wgb.movie.service.MovieService;
import com.wgb.movie.service.MovieShowService;
import com.wgb.movie.service.UserService;
import com.wgb.movie.utils.SqlUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 电影服务实现
 *
 * @author <a href="https://github.com/liyupi">程序员鱼皮</a>
 * @from <a href="https://www.code-nav.cn">编程导航学习圈</a>
 */
@Service
@Slf4j
public class MovieServiceImpl extends ServiceImpl<MovieMapper, Movie> implements MovieService {

    @Resource
    private UserService userService;

    /**
     * 校验数据
     *
     * @param movie
     * @param add   对创建的数据进行校验
     */
    @Override
    public void validMovie(Movie movie, boolean add) {
        ThrowUtils.throwIf(movie == null, ErrorCode.PARAMS_ERROR);
        // todo 从对象中取值
        String movieName = movie.getMovieName();
        Integer movieType = movie.getMovieType();
        String movieDesc = movie.getMovieDesc();
        Date releaseDate = movie.getReleaseDate();
        Integer movieTime = movie.getMovieTime();


        // 创建数据时，参数不能为空
        if (add) {
            // todo 补充校验规则
            ThrowUtils.throwIf(StringUtils.isBlank(movieName), ErrorCode.PARAMS_ERROR, "电影名称不能为空");
            ThrowUtils.throwIf(movieType == null, ErrorCode.PARAMS_ERROR, "电影类型不能为空");
            ThrowUtils.throwIf(StringUtils.isBlank(movieDesc), ErrorCode.PARAMS_ERROR, "电影描述不能为空");
            ThrowUtils.throwIf(releaseDate == null, ErrorCode.PARAMS_ERROR, "上映时间不能为空");
            ThrowUtils.throwIf(movieTime == null || movieTime <= 0, ErrorCode.PARAMS_ERROR, "电影时长不合法");
        }
        // 修改数据时，有参数则校验
        // todo 补充校验规则

    }

    /**
     * 获取查询条件
     *
     * @param movieQueryRequest
     * @return
     */
    @Override
    public QueryWrapper<Movie> getQueryWrapper(MovieQueryRequest movieQueryRequest) {
        QueryWrapper<Movie> queryWrapper = new QueryWrapper<>();
        if (movieQueryRequest == null) {
            return queryWrapper;
        }
        // todo 从对象中取值
        Long id = movieQueryRequest.getId();
        String movieName = movieQueryRequest.getMovieName();
        Integer movieType = movieQueryRequest.getMovieType();
        String movieDesc = movieQueryRequest.getMovieDesc();
        String movieIcon = movieQueryRequest.getMovieIcon();
        String sortField = movieQueryRequest.getSortField();
        String sortOrder = movieQueryRequest.getSortOrder();


        // todo 补充需要的查询条件

        // 模糊查询
        queryWrapper.like(StringUtils.isNotBlank(movieName), "movieName", movieName);
        queryWrapper.like(StringUtils.isNotBlank(movieDesc), "movieDesc", movieDesc);
        queryWrapper.like(StringUtils.isNotBlank(movieIcon), "movieIcon", movieIcon);

        // 精确查询
        queryWrapper.eq(ObjectUtils.isNotEmpty(id), "id", id);
        queryWrapper.eq(ObjectUtils.isNotEmpty(movieType), "movieType", movieType);
        // 排序规
        queryWrapper.orderBy(SqlUtils.validSortField(sortField),
                sortOrder.equals(CommonConstant.SORT_ORDER_ASC),
                sortField);
        return queryWrapper;
    }

    /**
     * 获取应用封装
     *
     * @param request
     * @return
     */
    @Override
    public MovieVO getMovieVO(Movie movie, HttpServletRequest request) {
        // 对象转封装类
        MovieVO movieVO = MovieVO.objToVo(movie);

        //  可以根据需要为封装对象补充值，不需要的内容可以删除
        // region 可选

        // endregion

        return movieVO;
    }

    /**
     * 分页获取电影封装
     *
     * @param moviePage
     * @param request
     * @return
     */
    @Override
    public Page<MovieVO> getMovieVOPage(Page<Movie> moviePage, HttpServletRequest request) {
        List<Movie> movieList = moviePage.getRecords();
        Page<MovieVO> movieVOPage = new Page<>(moviePage.getCurrent(), moviePage.getSize(), moviePage.getTotal());
        if (CollUtil.isEmpty(movieList)) {
            return movieVOPage;
        }
        // 对象列表 => 封装对象列表
        List<MovieVO> movieVOList = movieList.stream().map(movie -> {
            return MovieVO.objToVo(movie);
        }).collect(Collectors.toList());

        // todo 可以根据需要为封装对象补充值，不需要的内容可以删除
        // region 可选


        // endregion

        movieVOPage.setRecords(movieVOList);
        return movieVOPage;
    }

//    @Resource
//    private MovieShowService movieShowService;
//
//
//    @Override
//    @Transactional
//    public boolean removeByIdT(long id) {
//        movieShowService.remove(new QueryWrapper<MovieShow>().eq("movie_id", id));
//        return removeById(id);
//    }

}
